﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace IESN.MyControl
{
    [ToolboxBitmap(@"e:\dms_ico\TRFFC09.ICO")]
    public partial class Tank : Control
    {
        private Image _ImageFlgLockOv;
        private Image _ImagesEmptyLockOv;
        private Image _ImagesEmptyOv;
        private Image _ImagesFlgOv;
        private IContainer components;
        private bool mEnable;
        private string mFbarNumber;
        private float msngBorderWidth;
        private string mStageContent;
        private bool mStageNoColorChange;
        private float mStageNoFontSize;
        private string mStageNoString;
        private int mStageXPStopPoint;
        private TankStatus mStatus;
        private float mTextFontSize;
        private string mtextString;

        public event StatusChangedEventHandler StatusChangedEvent;

        public Tank()
        {
            base.Resize += new EventHandler(this.Alarm_Resize);
            base.Layout += new LayoutEventHandler(this.Alarm_Layout);
            this.mStatus = TankStatus.EmptyOv;
            this.msngBorderWidth = 1.5f;
            this.mtextString = "";
            this.mStageContent = "";
            this.mTextFontSize = 10f;
            this.mStageNoString = "";
            this.mStageNoFontSize = 10f;
            this.mEnable = true;
            this.mStageNoColorChange = false;
            this.mFbarNumber = "";
            this.InitializeComponent();
        }

        public Tank(Image EmptyOv, Image FlgOv, Image EmptyLockOv, Image FlgLockOv)
        {
            base.Resize += new EventHandler(this.Alarm_Resize);
            base.Layout += new LayoutEventHandler(this.Alarm_Layout);
            this.mStatus = TankStatus.EmptyOv;
            this.msngBorderWidth = 1.5f;
            this.mtextString = "";
            this.mStageContent = "";
            this.mTextFontSize = 10f;
            this.mStageNoString = "";
            this.mStageNoFontSize = 10f;
            this.mEnable = true;
            this.mStageNoColorChange = false;
            this.mFbarNumber = "";
            this.InitializeComponent();
            this.ImageEmptyOv = EmptyOv;
            this.ImageFlgOv = FlgOv;
            this.ImageEmptyLockOv = EmptyLockOv;
            this.ImageFlgLockOv = FlgLockOv;
        }

        private void Alarm_Layout(object sender, LayoutEventArgs e)
        {
            string affectedProperty = e.AffectedProperty;
            if (string.Equals(affectedProperty, "Bounds", StringComparison.CurrentCultureIgnoreCase))
            {
            }
        }

        private void Alarm_Resize(object sender, EventArgs e)
        {
            this.Invalidate();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void DrawLight(TankStatus ButtonStatus, Graphics grfGraphics)
        {
            int x = (int)Math.Round((double)(this.Size.Width * 0.01));
            int y = (int)Math.Round((double)(this.Size.Height * 0.01));
            int width = (int)Math.Round((double)(this.Size.Width * 0.98));
            int height = (int)Math.Round((double)(((double)(this.Size.Height * 3)) / 4.0));
            Rectangle rect = new Rectangle(x, y, width, height);
            if (ButtonStatus == TankStatus.EmptyOv && this.ImageEmptyOv != null)
            {
                grfGraphics.DrawImage(this.ImageEmptyOv, rect);
            }
            if (ButtonStatus == TankStatus.FlgOv && this.ImageFlgOv != null)
            {
                grfGraphics.DrawImage(this.ImageFlgOv, rect);
            }
            if (ButtonStatus == TankStatus.EmptyLockOv && this.ImageEmptyLockOv != null)
            {
                grfGraphics.DrawImage(this.ImageEmptyLockOv, rect);
            }
            if (ButtonStatus == TankStatus.FlgLockOv && this.ImageFlgLockOv != null)
            {
                grfGraphics.DrawImage(this.ImageFlgLockOv, rect);
            }
        }

        //[DebuggerStepThrough]
        private void InitializeComponent()
        {
            this.Name = "UserControl1";
            Size size = new Size(0x128, 0x128);
            this.Size = size;
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            Graphics grfGraphics = pe.Graphics;
            Font font = new Font("Times New Roman", this.StageNoFontSize, FontStyle.Regular);
            Brush black = Brushes.Black;
            font = new Font("Times New Roman", this.StageNoFontSize, FontStyle.Regular);
            if (this.mStageNoColorChange)
            {
                black = Brushes.Red;
            }
            else
            {
                black = Brushes.Black;
            }
            SizeF ef = grfGraphics.MeasureString(this.mStageNoString, font);
            PointF point = new PointF((float)Convert.ToInt16((double)(this.Width * 0.1)), (float)Convert.ToInt16((double)(this.Height * 0.75)));
            grfGraphics.DrawString(this.StageNoString, font, black, point);
            point = new PointF((float)Convert.ToInt16((double)(this.Width * 0.15)), (float)Convert.ToInt16((double)(this.Height * 0.86)));
            grfGraphics.DrawString(this.FbarNumber, font, black, point);
            this.DrawLight(this.Status, grfGraphics);
            grfGraphics.Dispose();
        }

        public void ResetStatus()
        {
            this.Status = TankStatus.EmptyOv;
        }

        public bool ShouldSerializeStatus()
        {
            if (this.mStatus == TankStatus.EmptyOv)
            {
                return false;
            }
            return true;
        }

        [DefaultValue((float)1f), Description("周围边框的宽度")]
        public float BorderWidth
        {
            get
            {
                return this.msngBorderWidth;
            }
            set
            {
                if (this.msngBorderWidth != value)
                {
                    this.msngBorderWidth = value;
                    this.Invalidate();
                }
            }
        }

        public bool enable
        {
            get
            {
                return this.mEnable;
            }
            set
            {
                this.mEnable = value;
            }
        }

        public string FbarNumber
        {
            get
            {
                return this.mFbarNumber;
            }
            set
            {
                if (string.Equals(this.mFbarNumber, value, StringComparison.CurrentCultureIgnoreCase))
                {
                    this.mFbarNumber = value;
                    this.Invalidate();
                }
            }
        }

        public Image ImageEmptyLockOv
        {
            get
            {
                return this._ImagesEmptyLockOv;
            }
            set
            {
                this._ImagesEmptyLockOv = value;
            }
        }

        public Image ImageEmptyOv
        {
            get
            {
                return this._ImagesEmptyOv;
            }
            set
            {
                this._ImagesEmptyOv = value;
            }
        }

        public Image ImageFlgLockOv
        {
            get
            {
                return this._ImageFlgLockOv;
            }
            set
            {
                this._ImageFlgLockOv = value;
            }
        }

        public Image ImageFlgOv
        {
            get
            {
                return this._ImagesFlgOv;
            }
            set
            {
                this._ImagesFlgOv = value;
            }
        }

        public string StageContent
        {
            get
            {
                return this.mStageContent;
            }
            set
            {
                this.mStageContent = value;
            }
        }

        public bool StageNoColorChange
        {
            get
            {
                return this.mStageNoColorChange;
            }
            set
            {
                if (this.mStageNoColorChange != value)
                {
                    this.mStageNoColorChange = value;
                    this.Invalidate();
                }
            }
        }

        public float StageNoFontSize
        {
            get
            {
                return this.mStageNoFontSize;
            }
            set
            {
                if (this.mStageNoFontSize != value)
                {
                    this.mStageNoFontSize = value;
                    this.Invalidate();
                }
            }
        }

        public string StageNoString
        {
            get
            {
                return this.mStageNoString;
            }
            set
            {
                if (string.Equals(this.mStageNoString, value, StringComparison.CurrentCultureIgnoreCase))
                {
                    this.mStageNoString = value;
                    this.Invalidate();
                }
            }
        }

        public int StageXPStopPoint
        {
            get
            {
                return this.mStageXPStopPoint;
            }
            set
            {
                this.mStageXPStopPoint = value;
            }
        }

        [Description("状态")]
        public TankStatus Status
        {
            get
            {
                return this.mStatus;
            }
            set
            {
                if (this.mStatus != value)
                {
                    this.mStatus = value;
                    if (this.StatusChangedEvent != null)
                    {
                        this.StatusChangedEvent(this, this.mStatus);
                    }
                    this.Invalidate();
                }
            }
        }

        public float TextFontSize
        {
            get
            {
                return this.mTextFontSize;
            }
            set
            {
                if (this.mTextFontSize != value)
                {
                    this.mTextFontSize = value;
                    this.Invalidate();
                }
            }
        }

        public string TextString
        {
            get
            {
                return this.mtextString;
            }
            set
            {
                if (string.Equals(this.mtextString, value, StringComparison.CurrentCultureIgnoreCase))
                {
                    this.mtextString = value;
                    this.Invalidate();
                }
            }
        }

        public delegate void StatusChangedEventHandler(object Sender, Tank.TankStatus NewStatus);

        public enum TankStatus
        {
            EmptyLockOv = 3,
            EmptyOv = 1,
            FlgLockOv = 4,
            FlgOv = 2
        }
    }
}
